package com.xique.park.controller.web;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.park.bean.request.web.record.SettlementRecordRequest;
import com.xique.park.service.service.IParkSettlementRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 结算记录 controller
 *
 * @author WLD
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/settlementRecord")
public class WebParkSettlementRecordController {

    private final IParkSettlementRecordService parkSettlementRecordService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody(required = false) SettlementRecordRequest settlementRecordRequest) {
        settlementRecordRequest = checkParam(settlementRecordRequest);
        PageResult pageResult = parkSettlementRecordService.selectPageList(settlementRecordRequest);
        return AjaxResult.success(pageResult);
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(@RequestBody(required = false) SettlementRecordRequest settlementRecordRequest, HttpServletResponse response) {
        settlementRecordRequest = checkParam(settlementRecordRequest);
        parkSettlementRecordService.exportList(settlementRecordRequest, response);
    }

    private SettlementRecordRequest checkParam(SettlementRecordRequest request) {
        if (request == null) {
            request = new SettlementRecordRequest();
        }
        int pageNum = request.getPageNum() == null ? 1 : request.getPageNum();
        int pageSize = request.getPageSize() == null ? 10 : request.getPageSize();
        if (request.getParkId() == null) {
            Long parkId = SecurityUtils.getParkId();
            if (parkId != null) {
                request.setParkId(parkId);
            }
        }
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        return request;
    }

}
